package se.thoka.twt.client.utils;

/**
 * @author Thobias Karlsson, Thoka
 * @since 8 jan 2010
 */
public class StringUtils {
	/**
	 * Check if a string is <code>null</code> or empty.
	 * 
	 * @param str The string.
	 * @return <code>true</code> if <code>null</code> or empty.
	 */
	public static boolean nullOrEmpty(String str) {
		return str == null || "".equals(str);
	}

	/**
	 * Compares two strings
	 * 
	 * @param s1 May be <code>null</code>
	 * @param s2 May be <code>null</code>
	 * @return The result
	 */
	public static int compare(String s1, String s2) {
		int result = 0;
		if (s1 != null) {
			if (s2 != null) {
				result = s1.compareTo(s2);
			} else {
				result = 1;
			}
		} else if (s2 != null) {
			result = -1;
		}

		return result;
	}

	/**
	 * Compares two strings
	 * 
	 * @param s1 May be <code>null</code>
	 * @param s2 May be <code>null</code>
	 * @param caseSensitive Set <code>false</code> to compare as case insensitive
	 * @return The result
	 */
	public static int compare(String s1, String s2, boolean caseSensitive) {
		if (caseSensitive) {
			return compare(s1, s2);
		}
		return compare(lowerCase(s1), lowerCase(s2));
	}

	/**
	 * Compares two trimmed
	 * 
	 * @param s1 May be <code>null</code>
	 * @param s2 May be <code>null</code>
	 * @return The result
	 */
	public static int compareAsTrimmed(String s1, String s2) {
		return compare(trim(s1), trim(s2));
	}

	/**
	 * Compares two trimmed strings
	 * 
	 * @param s1 May be <code>null</code>
	 * @param s2 May be <code>null</code>
	 * @param caseSensitive Set <code>false</code> to compare as case insensitive
	 * @return The result
	 */
	public static int compareAsTrimmed(String s1, String s2, boolean caseSensitive) {
		return compare(trim(s1), trim(s2), caseSensitive);
	}

	/**
	 * Trims a string
	 * 
	 * @param s The string to trim, may be <code>null</code>
	 * @return The trimmed string or <code>null</code> if supplied string was <code>null</code>
	 */
	public static String trim(String s) {
		return s != null ? s.trim() : null;
	}

	/**
	 * Checks if two strings are equal (both null or same string value)
	 * 
	 * @param s1 String 1, may be <code>null</code>
	 * @param s2 String 2, may be <code>null</code>
	 * @return <code>true</code> if the strings are equal
	 */
	public static boolean equal(String s1, String s2) {
		return equal(s1, s2, false);
	}

	/**
	 * Checks if two strings are equal (both null or same string value)
	 * 
	 * @param s1 String 1, may be <code>null</code>
	 * @param s2 String 2, may be <code>null</code>
	 * @param trimmed Set <code>true</code> to compare as trimmed
	 * @return <code>true</code> if the strings are equal
	 */
	public static boolean equal(String s1, String s2, boolean trimmed) {
		if (trimmed) {
			s1 = trim(s1);
			s2 = trim(s2);
		}
		return (s1 == null && s2 == null) || (s1 != null && s2 != null && s1.equals(s2));
	}

	/**
	 * @param s The string
	 * @return The supplied string with transformed to only lower case
	 */
	public static String lowerCase(String s) {
		return s != null ? s.toLowerCase() : null;
	}

	/**
	 * Checks if <code>haystack</code> contains the string <code>needle</code>.
	 * <p>
	 * <b>Note: This method is case sensitive.</b>
	 * </p>
	 * 
	 * @param haystack The hay stack.
	 * @param needle The needle.
	 * @return <code>true</code> if <code>haystack</code> contains <code>needle</code>.
	 */
	public static boolean contains(String haystack, String needle) {
		boolean contains = false;
		if (haystack != null && needle != null) {
			contains = haystack.indexOf(needle) != -1;
		}
		return contains;
	}

	/**
	 * Checks if <code>haystack</code> contains the string <code>needle</code>, ignoring case.
	 * 
	 * @param haystack The hay stack.
	 * @param needle The needle.
	 * @return <code>true</code> if <code>haystack</code> contains <code>needle</code>.
	 */
	public static boolean containsIgnoreCase(String haystack, String needle) {
		boolean contains = false;
		if (haystack != null && needle != null) {
			contains = haystack.toLowerCase().indexOf(needle.toLowerCase()) != -1;
		}
		return contains;
	}

	/**
	 * Checks if <code>haystack</code> contains all strings in <code>needles</code>, ignoring case.
	 * 
	 * @param haystack The hay stack.
	 * @param needles The needles.
	 * @return <code>true</code> if <code>haystack</code> contains all strings from </code>needles</code>.
	 */
	public static boolean containsAllIgnoreCase(String haystack, String[] needles) {
		boolean contains = false;
		if (haystack != null && needles != null) {
			contains = true;
			for (int i = 0; i < needles.length && contains; i++) {
				contains = containsIgnoreCase(haystack, needles[i]);
			}
		}
		return contains;
	}
	
	public static String zeroPad(long value, int minLen) {
		StringBuilder sb = new StringBuilder();
		String strValue = String.valueOf(value);
		while (sb.length() + strValue.length() < minLen) {
			sb.append('0');
		}
		sb.append(strValue);
		
		return sb.toString();
	}
}
